<?php
/**
 * Created by PhpStorm.
 * User: captain
 * Date: 2017/5/18
 * Time: 11:08
 */

namespace Admin\Controller;


use Think\Controller;
use Think\Upload;

class FrontController extends Controller
{
    //首页配置页面
    public function indexPage(){
        $obj = M("shouye");
        $arr = $obj->where('type=1')->find();
        $arr2 = $obj->where('type=2')->find();
        $this->assign('arr',$arr);
        $this->assign('arr2',$arr2);
        $this->display('index');
    }

    //名医主刀新增广告
    public function shouAdd2()
    {
        $data["link"] = $_POST["link"];
        $data["word"] = $_POST["word"];
        $data['type'] = $_POST['type'];
        $timestamp = time();
        $upload = new Upload();
        $upload->maxSize = 3145728;
        $upload->exts = array('jpg', 'png', 'jpeg');
        $upload->rootPath = "./Public/img/wp/";
        $upload->savePath = "";
        $upload->saveName = "act-".$timestamp;
        $upload->autoSub = false;
        $info = $upload->uploadOne($_FILES['pic']);
        $data["img"] = $info["savename"];
        $data["updated"] = $timestamp;
        $obj = M("shouye");
        $cid = $obj->add($data);
        if (!empty($cid)) {
            $this->success("添加成功", "linkList");
        } else {
            $this->error("添加失败", "linkList");
        }
    }

    //名医主刀删除广告
    public function delAct(){
        $id = $_POST['id'];
        $obj = M("shouye");
        $rs = $obj->where('id='.$id)->delete();
        if($rs ==1){
            $arr = ['code' =>0];
        }else{
            $arr = ['code'=>1];
        }
        $this->ajaxReturn($arr);
    }

    //孕产服务新增广告
    public function shouAdd3()
    {
        $data["link"] = $_POST["link"];
        $data["word"] = $_POST["word"];
        $data['type'] = $_POST['type'];
        $timestamp = time();
        $upload = new Upload();
        $upload->maxSize = 3145728;
        $upload->exts = array('jpg', 'png', 'jpeg');
        $upload->rootPath = "./Public/img/wp/";
        $upload->savePath = "";
        $upload->saveName = "act-".$timestamp;
        $upload->autoSub = false;
        $info = $upload->uploadOne($_FILES['pic']);
        $data["img"] = $info["savename"];
        $data["updated"] = $timestamp;
        $obj = M("shouye");
        $cid = $obj->add($data);
        if (!empty($cid)) {
            $this->success("添加成功", "indexPage");
        } else {
            $this->error("添加失败", "indexPage");
        }
    }

    //孕产服务删除广告
    public function delAct2(){
        $id = $_POST['id'];
        $obj = M("shouye");
        $rs = $obj->where('id='.$id)->delete();
        if($rs ==1){
            $arr = ['code' =>0];
        }else{
            $arr = ['code'=>1];
        }
        $this->ajaxReturn($arr);
    }

    //添加医院医生
    public function addDocterPage(){
        $obj = M('dept');
        $arr = $obj->select();
        $this->assign('datas',$arr);
        $this->display('addDocter');
    }

    //医院医生列表
    public function docterList(){
        $obj = M("doctor");
        $count = $obj->count();
        $p = getPage($count);
        $datas = $obj->limit($p->firstRow, $p->listRows)->select();
        $arr = [];
        foreach ($datas as $da){
            $da['desc'] = mb_substr($da['desc'],0,10,'utf-8')."...";
//            var_dump(substr($da['img'],0,4));exit();
            if(substr($da['img'],0,4) == "http"){
                $da['sta'] = 1;
            }else{
                $da['sta'] =2;
            }
            $arr[] = $da;
        }

        $this->assign('datas',$arr);
        $this->assign("page", $p->show());
        $this->display();
    }

    //添加医生
    public function addDoctor(){
        $data["name"] = $_POST["name"];
        $data["city"] = $_POST["city"];
        $data['a_title'] = $_POST['a_title'];
        $data['m_title'] = $_POST['m_title'];
        $data['hospital'] = $_POST['hospital'];
        $data['dept_name'] = $_POST['dept_name'];
        $data['desc'] = $_POST['desc'];
        $data['dept_id'] = $_POST['dept_id'];
        $timestamp = time();
        $upload = new Upload();
        $upload->maxSize = 3145728;
        $upload->exts = array('jpg', 'png', 'jpeg');
        $upload->rootPath = "./Public/img/wp/";
        $upload->savePath = "";
        $upload->saveName = "doc-".$timestamp;
        $upload->autoSub = false;
        $info = $upload->uploadOne($_FILES['pic']);
        $data["img"] =$info["savename"];
        $data["updated"] = $timestamp;
        $obj = M("doctor");
        $cid = $obj->add($data);
        if (!empty($cid)) {
            $this->success("添加成功", "docterList");
        } else {
            $this->error("添加失败", "docterList");
        }

    }

    public function editDoctorPage($id){
        $obj = M("doctor");
        $datas = $obj->where("id=".$id)->find();
        $obj2 = M('dept');
        $arr = $obj2->select();
        if(isset($datas['dept_id'])){
            $arr2 = $obj2->where("id=".$datas['dept_id'])->find();
            $this->assign('de',$arr2);
        }
        $this->assign('datas',$arr);
        $this->assign('da',$datas);
        $this->display('editDoctor');
    }

    public function editDoctor(){
        $data["name"] = $_POST["name"];
        $data["city"] = $_POST["city"];
        $data['a_title'] = $_POST['a_title'];
        $data['m_title'] = $_POST['m_title'];
        $data['hospital'] = $_POST['hospital'];
        $data['dept_name'] = $_POST['dept_name'];
        $data['desc'] = $_POST['desc'];
//        var_dump($data['desc']);exit();
        $data['dept_id'] = $_POST['dept_id'];
        $timestamp = time();
        $upload = new Upload();
        $upload->maxSize = 3145728;
        $upload->exts = array('jpg', 'png', 'jpeg');
        $upload->rootPath = "./Public/img/wp/";
        $upload->savePath = "";
        $upload->saveName = "doc-".$timestamp;
        $upload->autoSub = false;
        $info = $upload->uploadOne($_FILES['pic']);
        if(!empty($info)){
            $data["img"] =$info["savename"];
        }
        $data["updated"] = $timestamp;
        $obj = M("doctor");
        $cid = $obj->where("id=".$_POST['id'])->save($data);
        if ($cid>0 || $cid == 0) {
            $this->success("修改成功", "docterList");
        } else {
            $this->error("修改失败", "docterList");
        }
    }

    public function delDoctor($id){
        $obj = M("doctor");
        $arr = $obj->where("id=".$id)->find();
        if(isset($arr['img'])){
            if(substr($arr['img'],0,4) != "http"){
                unlink("./Public/img/wp/".$arr['img']);
            }
        }
        $rs = $obj->where("id=".$id)->delete();
        if($rs=1){
            $this->success("删除成功", "../../docterList");
        }else{
            $this->error("删除失败", "../../docterList");
        }
    }

    public function linkList(){
        $obj = M("shouye");
        $count = $obj->count();
        $p = getPage($count);
        $datas = $obj->limit($p->firstRow, $p->listRows)->select();

        $arr = [];
        foreach ($datas as $da){
            switch ($da['type']){
                case 1:
                    $da['name'] = '名医主刀';
                    break;
                case 2:
                    $da['name'] = '孕产服务';
                    break;
                case 3:
                    $da['name'] = '手术专题';
                    break;
                case 4:
                    $da['name'] = '名医故事';
                    break;
                default:
                    $da['name'] = '名医主刀';
            }
            $da['link'] = mb_substr($da['link'],0,20,'utf-8')."...";
           $arr[] = $da;
        }
        $this->assign("page", $p->show());
        $this->assign('datas',$arr);
        $this->display();
    }

    public function editLinkPage($id){
        $obj = M("shouye");
        $datas = $obj->where("id=".$id)->find();
        $this->assign('da',$datas);
        $this->display('editLink');

    }

    public function editLink(){
        $data["link"] = $_POST["link"];
        $data["word"] = $_POST["word"];
        $data['type'] = $_POST['type'];
        $timestamp = time();
        $upload = new Upload();
        $upload->maxSize = 3145728;
        $upload->exts = array('jpg', 'png', 'jpeg');
        $upload->rootPath = "./Public/img/wp/";
        $upload->savePath = "";
        $upload->saveName = "act-".$timestamp;
        $upload->autoSub = false;
        $info = $upload->uploadOne($_FILES['pic']);
        if(!empty($info)){
            $data["img"] = $info["savename"];
        }
        $data["updated"] = $timestamp;
        $obj = M("shouye");
        $cid = $obj->where("id=".$_POST['id'])->save($data);
        if (!empty($cid)) {
            $this->success("编辑成功", "linkList");
        } else {
            $this->error("编辑失败", "linkList");
        }
    }

    public function delLink($id){
        $obj = M("shouye");
        $arr = $obj->where("id=".$id)->find();
        if(isset($arr['img'])){
            unlink("./Public/img/wp/".$arr['img']);
        }
        $rs = $obj->where("id=".$id)->delete();
        if($rs=1){
            $this->success("删除成功","../../linkList");
        }else{
            $this->error("删除失败", "../../linkList");
        }
    }

    //医院医生列表
    public function hosList(){
        $obj = M("hospital");
        $count = $obj->count();
        $p = getPage($count);
        $datas = $obj->limit($p->firstRow, $p->listRows)->select();
        $arr = [];
        foreach ($datas as $da){
            switch ($da['type']){
                case 1:
                    $da['name'] = '名医主刀';
                    break;
                case 2:
                    $da['name'] = '孕产服务';
                    break;
                default:
                    $da['name'] = '名医主刀';
            }
            $da['desc'] = mb_substr($da['desc'],0,10,'utf-8')."...";
            $arr[] = $da;
        }

        $this->assign('datas',$arr);
        $this->assign("page", $p->show());
        $this->display();
    }

    public function addHosPage(){
//        $obj = M('hospital');
//        $arr = $obj->select();
//        $this->assign('datas',$arr);
        $this->display('addHos');
    }

    public function addHos(){
        $data['hospital'] = $_POST['hospital'];
        $data['tabel'] = $_POST['tabel'];
        $data['desc'] = $_POST['desc'];
        $data['type'] = $_POST['type'];
        $timestamp = time();
        $upload = new Upload();
        $upload->maxSize = 3145728;
        $upload->exts = array('jpg', 'png', 'jpeg');
        $upload->rootPath = "./Public/img/wp/";
        $upload->savePath = "";
        $upload->saveName = "hos-".$timestamp;
        $upload->autoSub = false;
        $info = $upload->uploadOne($_FILES['pic']);
        $data["img"] = $info["savename"];
        $data["updated"] = $timestamp;
        $obj = M("hospital");
        $cid = $obj->add($data);
        if (!empty($cid)) {
            $this->success("添加成功", "hosList");
        } else {
            $this->error("添加失败", "hosList");
        }
    }

    public function editHosPage($id){
        $obj = M("hospital");
        $datas = $obj->where("hid=".$id)->find();
        switch ($datas['type']){
            case 1:
                $datas['name'] = '名医主刀';
                break;
            case 2:
                $datas['name'] = '孕产服务';
                break;
            default:
                $datas['name'] = '名医主刀';
        }
        $this->assign('da',$datas);
        $this->display('editHos');
    }

    public function editHos(){
        $data['hospital'] = $_POST['hospital'];
        $data['tabel'] = $_POST['tabel'];
        $data['desc'] = $_POST['desc'];
        $data['type'] = $_POST['type'];
        $timestamp = time();
        $upload = new Upload();
        $upload->maxSize = 3145728;
        $upload->exts = array('jpg', 'png', 'jpeg');
        $upload->rootPath = "./Public/img/wp/";
        $upload->savePath = "";
        $upload->saveName = "act-".$timestamp;
        $upload->autoSub = false;
        $info = $upload->uploadOne($_FILES['pic']);
        if(!empty($info)){
            $data["img"] = $info["savename"];
        }
        $data["updated"] = $timestamp;
        $obj = M("hospital");
        $cid = $obj->where("hid=".$_POST['id'])->save($data);
        if (!empty($cid)) {
            $this->success("编辑成功", "hosList");
        } else {
            $this->error("编辑失败", "hosList");
        }
    }

    public function delHos($id){
        $obj = M("hospital");
        $arr = $obj->where("hid=".$id)->find();
        if(isset($arr['img'])){
                unlink("./Public/img/wp/".$arr['img']);
        }
        $rs = $obj->where("hid=".$id)->delete();
        if($rs=1){
            $this->success("删除成功","../../hosList");
        }else{
            $this->error("删除失败", "../../hosList");
        }
    }
}